package com.es.code.commons;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 接口返回结果辅助类
 *
 * @author wangjierj
 */
@ApiModel(value = "返回结果")
public class ResultData<T> {
    /**
     * 状态码：1成功，其他为失败
     */
    @ApiModelProperty(value = "返回状态编码")
    public String code;

    /**
     * 成功为success，其他为失败原因
     */
    @ApiModelProperty(value = "返回状态信息")
    public String message;

    /**
     * 数据结果集
     */
    @ApiModelProperty(value = "返回结果集")
    public T data;

    @ApiModelProperty(value = "成功标记")
    public boolean success;

    public ResultData() {
        this.code = ResultConstant.SUCCESS.code;
        this.message = ResultConstant.SUCCESS.message;
        this.data = null;
        this.success = true;
    }

    public ResultData(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        if (code == ResultConstant.SUCCESS.code) {
            this.success = true;
        } else {
            this.success = false;
        }
    }

    public ResultData(ResultConstant coResultConstant, T data) {
        this.code = coResultConstant.code;
        this.message = coResultConstant.message;
        this.data = data;
        if (coResultConstant.getCode() == ResultConstant.SUCCESS.code) {
            this.success = true;
        } else {
            this.success = false;
        }
    }

    public void setCoResultConstant(ResultConstant coResultConstant) {
        this.code = coResultConstant.code;
        this.message = coResultConstant.message;
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("CoResult{");
        sb.append("code='").append(this.getCode()).append('\'');
        sb.append(", message='").append(this.getMessage()).append('\'');
        sb.append(", data='").append(this.getData() != null ? this.getData().toString() : "data is null").append('\'');
        sb.append('}');
        return sb.toString();
    }
}
